package com.lixy.demo.springcloud.ch5_2.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.client.RestTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lixy.demo.springcloud.ch5_2.vo.User;

@RestController
public class MovieController {

	@GetMapping("/user/{id}")
	public User findById( @PathVariable Long id ) {
		return this.template.getForObject( "http://microservice-provider-user/" + id, User.class );
	}
	
	@GetMapping("/log-user-instance")
	public void logUserInstance() {
		ServiceInstance instance = this.client.choose( "microservice-provider-user" );
		MovieController.LOGGER.info( "{}:{}:{}", instance.getServiceId(), instance.getHost(), instance.getPort() );
	}
	
	@Autowired
	private RestTemplate template;
	
	@Autowired
	private LoadBalancerClient client;
	
	private static final Logger LOGGER = LoggerFactory.getLogger( MovieController.class );
}
